package MazeSolver;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class MapPanel extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = -5941047835867039238L;
	private BufferedImage maze;
	private BufferedImage overlay;
	private double transparency = .50;
	private Point start;
	private Point end;

	public MapPanel(String filepath) {
		try {
			maze = ImageIO.read(new File(filepath));
			overlay = new BufferedImage(maze.getWidth(),maze.getHeight(),BufferedImage.TYPE_INT_ARGB);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			maze = null;
		}
		
		start = null;
		end = null;
	}
	
	public void setOverlayColor(int x, int y,Color c) {
		overlay.setRGB(x, y, getOverlayRGB(c, transparency));
	}
	
	public BufferedImage getMaze() {
		return maze;
	}

	public void setMaze(BufferedImage maze) {
		this.maze = maze;
	}

	public BufferedImage getOverlay() {
		return overlay;
	}

	public void setOverlay(BufferedImage overlay) {
		this.overlay = overlay;
	}

	public double getTransparency() {
		return transparency;
	}

	public void setTransparency(double transparency) {
		this.transparency = transparency;
	}

	private int getOverlayRGB(Color color, double a) {
		int alpha = (int) (255*a);
		int rgb = color.getRGB();

        int mc = (alpha << 24) | 0x00ffffff;
        int newcolor = rgb & mc;
		return newcolor;
	}

	@Override
	public void paintComponent(Graphics g){
		Image buffer = new BufferedImage(maze.getWidth(),maze.getHeight(),BufferedImage.TYPE_INT_ARGB);
		Graphics bGraphics = buffer.getGraphics();
		if(maze != null) {
			bGraphics.drawImage(maze, 0, 0, null);
			bGraphics.drawImage(overlay, 0, 0, null);
			if(start != null) {
				bGraphics.setColor(Color.cyan);
				bGraphics.fillOval(start.getX() - 2, start.getY() - 2, 5, 5);
			}
			
			if(end != null) {
				bGraphics.setColor(Color.red);
				bGraphics.fillOval(end.getX() -2 , end.getY() - 2, 5, 5);
			}
			g.drawImage(buffer,this.getWidth()/2 - maze.getWidth()/2, this.getHeight()/2 - maze.getHeight()/2, null);
		} else {
			FontMetrics metrics = g.getFontMetrics();
			g.drawString("No Image Loaded", this.getWidth()/2 - metrics.stringWidth("No Image Loaded")/2, this.getHeight()/2 - metrics.getHeight()/2);
		}
	}

	public Point getStart() {
		return start;
	}

	public void setStart(Point start) {
		this.start = start;
	}

	public Point getEnd() {
		return end;
	}

	public void setEnd(Point end) {
		this.end = end;
	}

	public void setOverlaySolidColor(int x, int y, Color c) {
		overlay.setRGB(x, y, c.getRGB());
	}

	public void reset() {
		overlay = new BufferedImage(maze.getWidth(),maze.getHeight(),BufferedImage.TYPE_INT_ARGB);
	}

}
